package com.wuwei.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.wuwei.gulimall.product.entity.AttrEntity;
import com.wuwei.gulimall.product.service.AttrAttrgroupRelationService;
import com.wuwei.gulimall.product.service.AttrService;
import com.wuwei.gulimall.product.service.CategoryService;
import com.wuwei.gulimall.product.vo.AttrGroupRelationVo;
import com.wuwei.gulimall.product.vo.AttrRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wuwei.gulimall.product.entity.AttrGroupEntity;
import com.wuwei.gulimall.product.service.AttrGroupService;
import com.wuwei.common.utils.PageUtils;
import com.wuwei.common.utils.R;



/**
 * 属性分组
 *
 * @author wuwei
 * @email 842063349@qq.com
 * @date 2022-05-25 14:45:07
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


    @Autowired
    private CategoryService categoryService;


    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @PostMapping("/attr/relation")
    public R saveRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveRelation(vos);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/noattr/relation")
    public R getNoRelation(@PathVariable Long attrgroupId,
                                   @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.getNoRelation(attrgroupId,params);
        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
        attrService.deleteRelation(attrGroupRelationVo);
        return R.ok();
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    public R getRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> data = attrService.getRelation(attrgroupId);
        return R.ok().put("data", data);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPageAttribute(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        if (catelogId!=null){
            Long[] parentPath = categoryService.getParentPath(catelogId);
            attrGroup.setCatelogPath(parentPath);
        }
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeAndUnRelated(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
